'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  //接受请求，作出响应
  async index() {
    let res = await this.ctx.service.user.memory()
    await this.ctx.render('web/index/index.tpl', res)
  }
  async home() {
    const { ctx } = this
    let data = await ctx.service.user.find(2)
    ctx.body = data
  }
  //三种参数 params query form
  async detail() {
    const {ctx } = this
    let params = ctx.query
    var tag = params.type
    await ctx.render('web/index/detail.tpl', { tag })
  }

  async mood() {
    const { ctx } = this
    var obj = { path: "/mood/index" }
    await ctx.render('web/index/mood.tpl', obj)
  }
  async memory() {
    const {
      ctx
    } = this
    var params = ctx.query
    var res = await ctx.service.user.memory(params.page, 2)
    if (params.tag == 3) {
      res.tag = 3
      await ctx.render('web/index/memory.tpl', res)
    } else if (params.tag == 12) {
      var res = await ctx.service.user.memory(params.page, 3)
      res.tag = 12
      await ctx.render('web/index/technology.tpl', res)
    }
  }
  async archives() {
    const {
      ctx
    } = this
    await ctx.render('web/index/archives.tpl')
  }
  async message() {
    const {
      ctx
    } = this
    await ctx.render('web/index/message.tpl')
  }
  async loginGitHub() {
    var {
      ctx
    } = this
    var dataStr = (new Date()).valueOf()
    var path = "https://github.com/login/oauth/authorize"
    path += "?client_id=ddec6b18f5d9a6842502"
    path += "&scope=user"
    path += "&state=" + dataStr
    ctx.response.redirect(path)
  }
  //获取用户信息
  async loginCallback() {
    var { ctx } = this
    await ctx.render("web/index/login.tpl")
    const result = await ctx.curl(githubConfig.access_token_url, {
      method: 'POST',
      contentType: "json",
      data: {
        client_id: githubConfig.client_ID,
        client_secret: githubConfig.client_Secret,
        code,
      },
      dataType: "json"
    })
    const userInfo = await ctx.curl(githubConfig.user_info_url, {
      method: "GET",
      dataType: "json",
      headers: {},
      data: {
        access_token: result.data.access_token,
        scope: "user"
      }
    })
    var res = ""
    if (userInfo.data.login) {
      res = userInfo.data.login + " " + userInfo.data.avatar_url
      var obj = {
        uname: userInfo.data.login,
        avatar: userInfo.data.avatar_url,
        email: userInfo.data.blog
      }
      await ctx.service.user.saveGithubUser(obj)
    } else {
      res = "error"
    }
    await ctx.cookies.set("userData", res, {
      maxAge: 1000 * 3600 * 24
    })
    ctx.session.userInfo = res
    ctx.body = ""
  }
  async isLoginForSsh() {
    var {
      ctx
    } = this
    var data = await ctx.cookies.get('userData')
    ctx.set("content-type","text/event-stream")
    ctx.body = `data: ${data}` + "\n\n"
  }
  async isLogin(){
    var { ctx,app } = this
    var userInfo = await app.redis.get("userInfo")
    var res = await ctx.service.admin.parseToken(userInfo)
    ctx.body = { code:200,data:res.data._userInfo}
  }
  async destory() {
    var { ctx, app } = this
    await app.redis.del("userInfo")
    ctx.body = { code: "200", msg: "用户退出成功" }
  }
}
module.exports = HomeController;
